package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductAttrGroupDao;
import cn.fastoo.common.dao.product.ProductAttrGroupDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupDao;
import cn.fastoo.common.entity.product.ProductAttrGroup;
import cn.fastoo.common.entity.product.ProductAttrGroup;
import cn.fastoo.common.entity.product.ProductCategoryAttrgroup;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/attr/group")
public class ProductAttrGroupController {

    @Autowired
    private ProductAttrGroupDao dao;
    @Autowired
    private ProductCategoryAttrgroupDao productCategoryAttrgroupDao;

    @RequestMapping
    public String index() {
        return "/product/attr/group/index";
    }



    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductAttrGroup newData, QuerySearch querySearch, String op,Long productCategoryId) {
        Search search = querySearch.getSearch();
        if(productCategoryId!=null){
            List<ProductCategoryAttrgroup> productCategoryAttrgroupList = productCategoryAttrgroupDao.findByProductCategoryId(productCategoryId);
            if(Collections3.isNotEmpty(productCategoryAttrgroupList)){
                List<String> ids = new ArrayList<String>();
                for (ProductCategoryAttrgroup p:productCategoryAttrgroupList) {
                    ids.add(p.getProductAttrGroupId().toString());
                }
                search.addSearchFilter("NOTIN_id",ids);
            }

        }
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductAttrGroup oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    newData.setVersion(oldData.getVersion());
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.find(search),querySearch);
    }



}
